package org.budo.weixin.work.api.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.budo.support.lang.util.ArrayUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * 审批模版
 * 
 * @author lmw
 */
@Slf4j
@Getter
@Setter
@ToString
@NoArgsConstructor
public class TemplateDetail implements Serializable {
    private static final long serialVersionUID = -7720527230308310664L;

    private String responseBody;

    public TemplateDetail(String responseBody) {
        this.responseBody = responseBody;
    }

    public String getTemplateName() {
        JSONObject parseObject = JSON.parseObject(this.getResponseBody());
        JSONArray template_names = parseObject.getJSONArray("template_names");
        JSONObject template_name = template_names.getJSONObject(0);
        return template_name.getString("text");
    }

    public Boolean isOk() {
        JSONObject parseObject = JSON.parseObject(this.getResponseBody());
        String errmsg = parseObject.getString("errmsg");
        return "ok".equalsIgnoreCase(errmsg);
    }

    public TemplateContent getTemplateContent(String id) {
        List<TemplateContent> templateContents = this.getTemplateContentList();

        for (TemplateContent templateContent : templateContents) {
            if (templateContent.matchId(id)) {
                return templateContent;
            }
        }

        log.info("#56 getTemplateContent, return null, id=" + id + ", responseBody=" + this.getResponseBody());
        return null;
    }

    public List<TemplateContent> getTemplateContentList() {
        List<TemplateContent> templateContents = new ArrayList<TemplateContent>();

        JSONObject parseObject = JSON.parseObject(this.getResponseBody());
        JSONObject template_content = parseObject.getJSONObject("template_content");
        JSONArray controls = template_content.getJSONArray("controls");

        for (Object each : controls) {
            JSONObject _control = (JSONObject) each;

            TemplateContent templateContent = new TemplateContent(_control);
            templateContents.add(templateContent);
        }

        return templateContents;
    }

    public Map<String, Object> titleToValueMapSkipByControl(Map<String, Object> properties, String[] skipByControl) {
        Map<String, Object> map = new HashMap<String, Object>();

        for (Entry<String, Object> entry : properties.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            // 根据 id 或 title 查找
            TemplateContent templateContent = this.getTemplateContent(key);
            if (null == templateContent || ArrayUtil.contains(skipByControl, templateContent.getControl())) {
                log.info("#87 templateContent = " + templateContent + ", key = " + key + ", properties = " + properties);
                continue;
            }

            Object valueText = templateContent.valueText(value);
            map.put(templateContent.getTitle(), valueText); // title to value map
        }

        return map;
    }
}
